#encoding:utf-8
import os,sys
from os import read
import pandas as pd
import warnings
import random
from gensim.models import Word2Vec
from pycw2vec.io.data_transformer import DataTransformer
from utils import readEmotionDict

from pycw2vec.config.word2vec_config import configs as config
warnings.filterwarnings("ignore")

def cw2vec(word,embedding_path=config['save_gensim_vector_path'],stroke2word_path=config['save_idx2word_path'],top_n=10):
    data_transformer = DataTransformer(embedding_path = embedding_path,
                                       stroke2word_path = stroke2word_path)
    words = data_transformer.get_similar_words(word = word, w_num = top_n)
    return words

def word2vec(word,model_path,top_n=10):
    w2c_model = Word2Vec.load(model_path)
    words = w2c_model.similar_by_word(word=word,topn=top_n)
    print(words)
    return words
def getEmotStatue(words,df_data):
    print("这里是getEmotStatue")
    for word in words:
        # print("-"*8)
        temp = df_data[df_data['词语'] == word[0]]
        if not temp.empty:
            print('--',temp.values)
        else:
            pass
            # print("-{}为空".format(word),end=', ')
    print("\n=======================")

def getEmotWord(n=2):
    df_simple_emo_dict,df_emotion_count = readEmotionDict()
    # 获取前n个情感类别，使用values转为numpy
    top_n_emot = df_emotion_count.head(n)['情感分类'].values
    print(top_n_emot.tolist(),type(top_n_emot))
    df_top_n_emot = df_simple_emo_dict[df_simple_emo_dict['情感分类'] == top_n_emot[0]]
    top_n_emot_words = df_top_n_emot['词语'].values
    
    # temp = df_simple_emo_dict[df_simple_emo_dict['词语']=='开心']
    # print(type(temp))
    # print(temp.empty)
    # print(temp)
    # sys.exit(1)
    # print(df_top_n_emot.head(7))
    # print(top_n_emot_words)
    # 随机挑选出一个词语
    choice_word = random.choices(top_n_emot_words,k=1)
    print(choice_word)
    choice_word[0] = '开心'
    print("---测试词----")
    print(choice_word[0])
    print("w2c测试结果")
    try:
        w2c_words = word2vec(choice_word[0])
        getEmotStatue(w2c_words,df_simple_emo_dict)
    except Exception as e:
        print(e)
    print("-"*8)
    print("cw2c测试结果")
    try:
        cw2c_words = cw2vec(choice_word[0],top_n=11)
        getEmotStatue(cw2c_words,df_simple_emo_dict)
    except Exception as e:
        print(e)

    

if __name__ == "__main__":
    print("hello")
    # cw2vec("快乐") 
    # print("----")
    wiki_model_path="/home/stu/Documents/dataset/wiki/w2c_300d/wiki.model"
    sogo_model_path = "/home/stu/Documents/dataset/sougo/sougoCA_full/sougoCA_200d/sougoCA.model"

    wiki_BASE = "/home/stu/Documents/dataset/wiki/cw2vec_200d/"
    wiki_cw_model_path = wiki_BASE + "embedding/gensim_word_vector.bin"
    wiki_cw_id2word_path = wiki_BASE+ "processed/idx2word.pkl"

    sougou_CA_base = "/home/stu/Documents/dataset/sougo/sougoCA_full/sougoCA_cw200d/"
    sougo_cw_model_path = sougou_CA_base +"embedding/gensim_word_vector.bin"
    sougo_cw_id2word_path = sougou_CA_base + "processed/idx2word.pkl"
    print("以下为wiki语料训练结果")
    word2vec(word="快乐",model_path=wiki_model_path)
    print()
    print("="*10)
    print("以下为sogou语料训练结果")
    word2vec(word="快乐",model_path=sogo_model_path)
    # getEmotWord()
    print("\n\n")
    print("-"*10)
    print("以下为wiki语料 按cw2vec训练结果")
    cw2vec(word="快乐",embedding_path = wiki_cw_model_path,stroke2word_path=wiki_cw_id2word_path)
    print()
    print("="*10)
    print("以下为sougo语料，按cw2vec训练结果")
    cw2vec(word="快乐",embedding_path=sougo_cw_model_path,stroke2word_path=sougo_cw_id2word_path)